package com.fubowen.lepton.ui.window

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.MarginLayoutParams
import android.widget.RelativeLayout
import androidx.annotation.IntDef
import androidx.core.view.marginTop
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
import androidx.navigation.fragment.findNavController
import com.fubowen.lepton.R
import com.fubowen.lepton.databinding.FragmentWindowItemBinding


private const val SHADOW = "isNeedShadow"
private const val IS_EXPAND = "isExpand"

class WindowItemFragment : Fragment() {

    private var marginTop: Int = 0
    private var paddingLeft: Int = 0
    private var paddingRight: Int = 0
    private var paddingTop: Int = 0
    private var paddingBottom: Int = 0
    private lateinit var binding: FragmentWindowItemBinding
    private var shadow: Int = TOP_BOTTOM_LEFT_RIGHT
    private var isExpand = false

    /**
     * 父级容器属性
     */
    private var parentContainer: RelativeLayout? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            shadow = it.getInt(SHADOW)
            isExpand = it.getBoolean(IS_EXPAND)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentWindowItemBinding.inflate(inflater, container, false)
        marginTop = binding.containerContent.marginTop
        paddingLeft = binding.root.paddingLeft
        paddingRight = binding.root.paddingRight
        paddingTop = binding.root.paddingTop
        paddingBottom = binding.root.paddingBottom
        setBackground(shadow)
        ff()
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        parentContainer = view.parent.parent as RelativeLayout
        if (isExpand) {
            expand()
        }
    }

    fun expand() {
        binding.root.setPadding(0, 0, 0, 0)
        setBackground(NO_SHADOW)
        binding.containerContent.updateLayoutParams<MarginLayoutParams> { topMargin = 0 }
        val fragment = childFragmentManager.findFragmentById(R.id.container_content)
        childFragmentManager.commit { setPrimaryNavigationFragment(fragment) }
    }

    fun shrink(@Shadow shadow: Int) {
        binding.root.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom)
        setBackground(shadow)
        binding.containerContent.updateLayoutParams<MarginLayoutParams> { topMargin = marginTop }
        childFragmentManager.commit { setPrimaryNavigationFragment(null) }
    }

    private fun setBackground(@Shadow isNeedShadow: Int) = when (isNeedShadow) {
        TOP_BOTTOM_LEFT_RIGHT -> binding.root.setBackgroundResource(R.drawable.border_shadow_top_bottom_left_right)
        TOP -> binding.root.setBackgroundResource(R.drawable.border_shadow_top)
        NO_SHADOW -> binding.root.setBackgroundColor(Color.WHITE)
        else -> {}
    }

    @SuppressLint("ClickableViewAccessibility")
    private fun ff() {
        var x1 = 0f
        var x2 = 0f
        binding.containerContent.setOnTouchListener { _, event ->
            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    x1 = event.x
                    false
                }

                MotionEvent.ACTION_UP -> {
                    x2 = event.x
                    val deltaX = x2 - x1
                    if (deltaX > 0) {
                        val fragment = childFragmentManager.findFragmentById(R.id.container_content)
                        val ddd = fragment?.findNavController()?.backQueue!!
                        ddd[0].id
                        ddd[0].destination
                        val ddddd = ddd.count()

                        val dd = 123
                    } else if (deltaX < 0) {
                        val dd = 123
                    }
                    true
                }
            }
            false
        }
    }

    companion object {
        @JvmStatic
        fun newInstance(@Shadow shadow: Int, isExpand: Boolean = false) =
            WindowItemFragment().apply {
                arguments = Bundle().apply {
                    putInt(SHADOW, shadow)
                    putBoolean(IS_EXPAND, isExpand)
                }
            }

        const val TOP_BOTTOM_LEFT_RIGHT = 2
        const val TOP = 4
        const val NO_SHADOW = 8

        @IntDef(TOP_BOTTOM_LEFT_RIGHT, TOP, NO_SHADOW)
        @Retention(AnnotationRetention.SOURCE)
        annotation class Shadow
    }
}